package com.xhtt.modules.app.controller.task;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.exception.RRExceptionEnum;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.ExportExcel;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.sys.controller.AbstractController;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.task.entity.TaskPotPlan;
import com.xhtt.modules.task.entity.TaskPotSeo;
import com.xhtt.modules.task.entity.TaskPotSeoLog;
import com.xhtt.modules.task.entity.TaskPotTask;
import com.xhtt.modules.task.model.TaskOperConnectModel;
import com.xhtt.modules.task.model.TaskOperModel;
import com.xhtt.modules.task.model.pot.ArrangeModel;
import com.xhtt.modules.task.model.pot.TaskOperCCRModel;
import com.xhtt.modules.task.model.pot.TaskOperOtherModel;
import com.xhtt.modules.task.service.TaskPotPlanService;
import com.xhtt.modules.task.service.TaskPotSeoLogService;
import com.xhtt.modules.task.service.TaskPotTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 罐计划 前端控制器
 * </p>
 *
 * @author feipc
 * @since 2019-03-05
 */
@Api(tags = "任务平台-罐计划", value = "任务平台-罐计划")
@RestController
@RequestMapping("app/task/pot")
public class TaskPotController extends AbstractController {

    @Autowired
    private TaskPotPlanService taskPotPlanService;
    @Autowired
    private TaskPotTaskService taskPotTaskService;
    @Autowired
    private TaskPotSeoLogService taskPotSeoLogService;

    /**
     * 获取罐计划列表
     */
    @GetMapping(value = "/planList")
    @ApiOperation("获取罐计划列表(web)")
    @Login
    public R planList(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        PageUtils page = taskPotPlanService.planList(params, user);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 新增罐计划
     */
    @PostMapping(value = "/addPlan")
    @ApiOperation("新增罐计划(web)")
    @Login
    public R addPlan(@RequestBody TaskPotPlan taskPotPlan) {
        taskPotPlanService.addPlan(taskPotPlan);
        return R.ok();
    }

    /**
     * 删除罐计划
     */
    @PostMapping(value = "/deletePlan")
    @ApiOperation("删除罐计划(web)")
    @Login
    public R deletePlan(@RequestBody Integer[] ids) {
        taskPotPlanService.deletePlan(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 修改罐计划
     */
    @PostMapping(value = "/updatePlan")
    @ApiOperation("修改罐计划(web)")
    @Login
    public R updatePlan(@RequestBody TaskPotPlan taskPotPlan) {
        taskPotPlanService.updatePlan(taskPotPlan);
        return R.ok();
    }

    /**
     * 获取罐计划详情
     */
    @GetMapping(value = "/getPlan/{id}")
    @ApiOperation("获取罐计划详情")
    @Login
    public R getPlan(@PathVariable("id") Integer id) {
        TaskPotPlan taskPotPlan = taskPotPlanService.getPlan(id);
        return R.ok().put(Constant.DATA, taskPotPlan);
    }

    /**
     * 任务安排弹出页面的展示内容
     */
    @GetMapping(value = "/getArrangeInfo")
    @ApiOperation("获取-任务安排弹出页面的展示内容(web)")
    @Login
    public R getArrangeInfo(@RequestParam Integer id) {
        ArrangeModel arrangeModel = taskPotPlanService.getArrangeInfo(id);
        return R.ok().put(Constant.DATA, arrangeModel);
    }

    /**
     * 任务安排提交
     */
    @PostMapping(value = "/arrange")
    @ApiOperation("任务安排提交（web）")
    @Login
    public R arrange(@RequestBody ArrangeModel arrangeModel) {
        if (null == arrangeModel.getPlanId()) {
            throw new RRException("参数有误");
        }
        taskPotPlanService.arrange(arrangeModel);
        return R.ok();
    }

    /**
     * 任务分配-退回
     */
    @PostMapping(value = "/back")
    @ApiOperation("任务分配-退回（web）")
    @Login
    public R back(@RequestBody TaskPotPlan plan) {
        taskPotPlanService.back(plan);
        return R.ok();
    }

    /**
     * 指派任务页面-查看任务
     */
    @GetMapping(value = "/getTaskDetail")
    @ApiOperation("指派任务页面-查看任务(web)")
    @Login
    public R getTaskDetail(@RequestParam Integer id, @ApiIgnore @LoginUser SysUserEntity user) {
        ArrangeModel am = taskPotTaskService.getTaskDetail(id, user);
        return R.ok().put(Constant.DATA, am);
    }

    /**
     * 指派任务
     */
    @PostMapping(value = "/pubTask")
    @ApiOperation("指派任务(web)")
    @Login
    public R pubTask(@RequestBody List<TaskPotTask> tasks, @ApiIgnore @LoginUser SysUserEntity user) {
        //其实是更新字段操作，所以判断一下id、
        if (null == tasks.get(0).getId()) {
            throw new RRException("参数有误");
        }
        taskPotTaskService.pubTask(tasks, user);
        return R.ok();
    }


    /**
     * 任务确认-任务列表
     */
    @GetMapping(value = "/confirmTaskList")
    @ApiOperation("任务确认-任务列表(web)")
    @Login
    public R confirmTaskList(@RequestParam Map<String, Object> params, @ApiIgnore @LoginUser SysUserEntity user) {
        params.put("confirmUserId", user.getUserId());
        PageUtils page = taskPotTaskService.confirmTaskList(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 任务确认-查看任务作业详情
     */
    @GetMapping(value = "/taskOperDetail")
    @ApiOperation("任务确认-查看任务作业详情(web/app)")
    @Login
    public R taskOperDetail(@RequestParam Integer taskId, @ApiIgnore @LoginUser SysUserEntity user) {
        TaskOperModel model = taskPotTaskService.taskOperDetail(taskId, user.getUserId().intValue());
        return R.ok().put(Constant.DATA, model);
    }

    /**
     * 查看CCR任务作业详情(多个)
     */
    @GetMapping(value = "/taskCCRDetail")
    @ApiOperation("查看CCR任务作业详情(多个)(web/app)")
    @Login
    public R taskCCRDetail(@RequestParam Integer taskId) {
        List<TaskOperCCRModel> modelList = taskPotTaskService.taskCCRDetail(taskId);
        return R.ok().put(Constant.DATA, modelList);
    }

    /**
     * 查看CCR任务作业状态
     */
    @GetMapping(value = "/taskCCRStatus")
    @ApiOperation("查看CCR任务作业状态(app)")
    @Login
    public R taskCCRStatus(@RequestParam Integer taskId) {
        String state = taskPotTaskService.taskCCRStatus(taskId);
        return R.ok().put(Constant.DATA, state);
    }

    /**
     * APP点击开始操作
     */
    @PostMapping(value = "/start")
    @ApiOperation("APP点击开始操作(app)")
    @Login
    public R start(Integer taskId) {
        taskPotTaskService.start(taskId);
        return R.ok();
    }

    /**
     * APP临时保存管线任务
     */
    @PostMapping(value = "/tempConnectSave")
    @ApiOperation("APP临时保存管线任务(app)")
    @Login
    public R tempConnectSave(@RequestBody TaskOperConnectModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskPotTaskService.tempSave(model);
        return R.ok();
    }

    /**
     * APP提交管线任务
     */
    @PostMapping(value = "/submitConnectTask")
    @ApiOperation("APP提交管线任务(app)")
    @Login
    public R submitConnectTask(@RequestBody TaskOperConnectModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskPotTaskService.submitTask(model);
        return R.ok();
    }

    /**
     * APP临时保存CCR任务
     */
    @PostMapping(value = "/tempCCRSave")
    @ApiOperation("APP临时保存CCR任务(app)")
    @Login
    public R tempCCRSave(@RequestBody TaskOperCCRModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskPotTaskService.tempSave(model);
        return R.ok();
    }

    /**
     * APP提交CCR任务
     */
    @PostMapping(value = "/submitCCRTask")
    @ApiOperation("APP提交CCR任务(app)")
    @Login
    public R submitCCRTask(@RequestBody TaskOperCCRModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskPotTaskService.submitTask(model);
        return R.ok();
    }

    /**
     * APP临时保存其他任务
     */
    @PostMapping(value = "/tempOtherSave")
    @ApiOperation("APP临时保存其他任务(app)")
    @Login
    public R tempOtherSave(@RequestBody TaskOperOtherModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskPotTaskService.tempSave(model);
        return R.ok();
    }

    /**
     * APP提交其他任务
     */
    @PostMapping(value = "/submitOtherTask")
    @ApiOperation("APP提交其他任务(app)")
    @Login
    public R submitOtherTask(@RequestBody TaskOperOtherModel model) {
        if (null == model.getTaskId()) {
            throw new RRException("参数有误");
        }
        taskPotTaskService.submitTask(model);
        return R.ok();
    }

    /**
     * 任务确认-任务确认的提交
     */
    @PostMapping(value = "/commitTask")
    @ApiOperation("任务确认的提交(web/app)")
    @Login
    public R commitTask(@RequestBody TaskOperModel model, @ApiIgnore @LoginUser SysUserEntity user) {
        taskPotTaskService.commitTask(model, user.getUserId().intValue());
        return R.ok();
    }

    /**
     * 倒罐任务一览表
     */
    @GetMapping(value = "/core/seo")
    @ApiOperation("倒罐任务一览表(web)")
    @Login
    public R seoList(@RequestParam Map<String, Object> params) {
        PageUtils page = taskPotPlanService.seoList(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 倒罐任务一览表编辑
     */
    @PostMapping(value = "/core/seoEdit")
    @ApiOperation("倒罐任务一览表编辑(web)")
    @Login
    public R seoEdit(@RequestBody TaskPotSeo seo, @ApiIgnore @LoginUser SysUserEntity user) {
        taskPotPlanService.seoEdit(seo, user);
        return R.ok();
    }

    /**
     * 倒罐任务一览表日志
     */
    @GetMapping(value = "/core/seoLog")
    @ApiOperation("倒罐任务一览表日志(web)")
    @Login
    public R seoLog(@RequestParam Integer id) {
        List<TaskPotSeoLog> list = taskPotSeoLogService.list(new LambdaQueryWrapper<TaskPotSeoLog>().eq(TaskPotSeoLog::getId, id));
        return R.ok().put(Constant.DATA, list);
    }

    /**
     * 倒罐任务一览表导出
     */
    @GetMapping(value = "/core/seoExport")
    @ApiOperation("倒罐任务一览表导出(web)")
    @Login
    public R seoExport(@RequestParam Map<String, Object> params, HttpServletResponse response) {
        params.put("limit", "65000");
        ExportExcel exportExcel = new ExportExcel();
        PageUtils page = taskPotPlanService.seoList(params);
        List list = page.getList();

        String fileName = "倒罐任务";
        String sheetName = "倒罐任务";
        String[] headers = {"日期", "转出罐", "转入罐", "产品", "班组", "软管连接", "通球", "CCR监控", "预计转罐时间", "开始接软管时间", "结束接软管时间", "开始通球时间", "结束通球时间", "状态"};
        String[] columns = {"date", "exportPot", "importPot", "productName", "teamName", "hoseStatusName", "ballStatusName", "ccrStatusName", "planStartDate", "hoseStartTime", "hoseFinishTime", "ballStartTime", "ballFinishTime", "planStatusName"};
        try {
            exportExcel.exportExcel(fileName, sheetName, headers, columns, list, response);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RRException(RRExceptionEnum.EXPORT_FAIL);
        }
        return R.ok().put(Constant.DATA, page);
    }

}
